package org.product.chat.controller;

import com.google.gson.Gson;
import org.product.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zqx
 * @date 2020-12-22
 */
@WebServlet("/api/chat/login.do")
public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private static List<String> userLists = new ArrayList<String>();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username") ;
		boolean isExist = userLists.contains(username) ;

		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter() ;

		if(isExist) {
			out.print(new Gson().toJson(errorJson("此用户已经存在")));
		} else {
			out.print(new Gson().toJson(successJson(username,200,"登录成功")));
			userLists.add(username) ;
		}
	}

}
